package ynu.edu.online_course.mapper;

import org.apache.ibatis.annotations.*;
import ynu.edu.online_course.entity.Banned;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface BannedMapper {

    // 插入冻结记录
    @Insert("INSERT INTO banned (user_id, operated_by, banned_at, unbanned_at) " +
            "VALUES (#{user.id}, #{operatedBy.id}, #{bannedAt}, #{unbannedAt})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insertBannedRecord(Banned banned);

    // 查询所有冻结记录
    @Select("SELECT * FROM banned")
    @Results({
            @Result(column = "id", property = "id"),
            @Result(column = "user_id", property = "user.id"),
            @Result(column = "operated_by", property = "operatedBy.id"),
            @Result(column = "banned_at", property = "bannedAt"),
            @Result(column = "unbanned_at", property = "unbannedAt")
    })
    List<Banned> findAll();

    // 查询某用户的冻结记录
    @Select("SELECT * FROM banned WHERE user_id = #{userId}")
    @Results({
            @Result(column = "id", property = "id"),
            @Result(column = "user_id", property = "user.id"),
            @Result(column = "operated_by", property = "operatedBy.id"),
            @Result(column = "banned_at", property = "bannedAt"),
            @Result(column = "unbanned_at", property = "unbannedAt")
    })
    List<Banned> findByUserId(@Param("userId") Long userId);
    @Select("SELECT * FROM banned WHERE unbanned_at <= #{now}")
    @Results({
            @Result(column = "id", property = "id"),
            @Result(column = "user_id", property = "user",
                    one = @One(select = "ynu.edu.online_course.mapper.UserMapper.findById")),
            @Result(column = "operated_by", property = "operatedBy",
                    one = @One(select = "ynu.edu.online_course.mapper.UserMapper.findById")),
            @Result(column = "banned_at", property = "bannedAt"),
            @Result(column = "unbanned_at", property = "unbannedAt")
    })
    List<Banned> findExpiredBans(@Param("now") LocalDateTime now);

    @Delete("DELETE FROM banned WHERE id = #{id}")
    int deleteById(@Param("id") Long id);

}
